package com.bayair.doctor.ui.inquiry;

import com.bayair.doctor.biz.im.ContactHelper;
import com.bayair.doctor.data.bean.ServerListResp;
import com.bayair.doctor.utils.log.KLog;
import com.tencent.imsdk.TIMFriendshipManager;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.imsdk.friendship.TIMFriend;
import com.tencent.qcloud.tim.uikit.modules.contact.ContactItemBean;

import java.util.ArrayList;
import java.util.List;

public class ContactDataKit {

    private static ContactDataKit instance = new ContactDataKit();

    private ContactDataKit() {
    }

    public static ContactDataKit instance() {
        return instance;
    }

    private ContactCallback mCallback;

    public void loadContact(ContactCallback callback) {
        this.mCallback = callback;
        List<TIMFriend> timFriends = TIMFriendshipManager.getInstance().queryFriendList();
        if (timFriends != null && !timFriends.isEmpty()) {
            update(timFriends);
        } else {
            TIMFriendshipManager.getInstance().getFriendList(new TIMValueCallBack<List<TIMFriend>>() {
                @Override
                public void onError(int code, String desc) {
                    KLog.d("getFriendList onError,code : " + code + ",  desc = " + desc);
                    if (mCallback != null) {
                        mCallback.setError();
                    }
                }

                @Override
                public void onSuccess(List<TIMFriend> timFriends) {
                    if (timFriends == null) {
                        timFriends = new ArrayList<>();
                    }
                    update(timFriends);
                }
            });
        }
    }

    private void update(List<TIMFriend> timFriends) {
        List<ContactItemBean> contactData = new ArrayList<>();
        for (TIMFriend timFriend : timFriends) {
            ContactItemBean info = new ContactItemBean();
            info.covertTIMFriend(timFriend);
            if (ContactHelper.isValid(patients, info.getId())) {
                info.setInServer(true);
                //只有服务中好友才能看到
            }
            contactData.add(info);
        }
        if (mCallback != null) {
            mCallback.setList(contactData);
        }
    }

    //------------------保存kit中服务属性----------------------------------------------------------
    /**
     在服务中患者（患者id对应会话id）
     */
    private List<ServerListResp.PatientsBean> patients;

    public void setListInServer(List<ServerListResp.PatientsBean> patients) {
        this.patients = patients;
    }

}